// pages/jiechedanbox/checkWokerSelect/checkWokerSelect.js
import {
  getBeginCheckInit,
  confirmBeginCheck
} from '../../../utils/networkRequest.js'
import { errorMsg } from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    beginCheckInfo: null,
    isShowDelay: false,
    carInfo: null,
    delayNum:"",
    checkOrderId:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    let carInfo = JSON.parse(options.carInfo);
    debugger;
    getBeginCheckInit(options.checkId, (r) => {
      console.log(r);
      if (r.code == 0) {
        self.setData({
          beginCheckInfo: r.data,
          carInfo: carInfo,
          checkOrderId: options.checkId
        })
      }
    })
  },
  //获取延迟推送天数
  getDelayChange: function (e) {
    this.setData({
      delayNum: e.detail.detail.value
    })
  },
  //选择选项
  onCheckItem: function(e) {
    let {
      beginCheckInfo
    } = this.data;
    let pindex = e.currentTarget.dataset.pindex;
    let checkindex = e.currentTarget.dataset.checkindex;
    let isShowDelay = false;

    beginCheckInfo.tobe_fixed[pindex].fixment[checkindex].isSelect = !beginCheckInfo.tobe_fixed[pindex].fixment[checkindex].isSelect


    beginCheckInfo.tobe_fixed.map((o, i) => {
      if (i == pindex) {
        o.fixment.map((oo, ii) => {
          if (ii == checkindex) {
            oo.isSelect = true;
          } else {
            oo.isSelect = false;
          }
        })
      }
    })

    beginCheckInfo.tobe_fixed.map((o, i) => {
      o.fixment.map((oo, ii) => {
        if (oo.id == 3 && oo.isSelect) {
          //有选择下次再说
          isShowDelay = true;
          return;
        }
      })
    })

    this.setData({
      beginCheckInfo: beginCheckInfo,
      isShowDelay: isShowDelay
    })
  },

  //确认施工
  onConfirmBeginCheck: function() {
    let { checkOrderId, beginCheckInfo, delayNum} = this.data
    let tempArr = [];
    // {
    //   "id": "15",
    //     "fixment_id": "3"
    // }
    beginCheckInfo.tobe_fixed.map((o, i) => {
        o.fixment.map((oo, ii) => {
          if (oo.isSelect) {
             tempArr.push({
               id:o.id,
               fixment_id:oo.id
             })
          }  
        }) 
    })
    debugger;
    confirmBeginCheck(checkOrderId, tempArr,delayNum,(r) => {
      if(r.code==0){
        wx.navigateBack()
      }else{
        errorMsg(r.message)
      }

    })
  }
})